﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BEGDC.PreContract.DAL;

namespace PreContractWeb.Task
{
    public partial class FeedbackHistoryList : System.Web.UI.Page
    {
        /// <summary>
        /// Get Pre-ContractID
        /// </summary>
        protected int PreContractID
        {
            get
            {
                string strPreContractID = Page.Request.QueryString["PreContractID"].ToString();
                return string.IsNullOrEmpty(strPreContractID) ? 0 : Convert.ToInt32(strPreContractID);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.BindPreContractBasicInfo();
                this.BindDataGrid();
            }

            if (!Page.User.IsInRole(BEGDC.PreContract.BLL.Role.EnumRole.Admin.ToString()))
            {
                this.gvFeedback.Columns[13].Visible = false;
            }
        }

        private void BindPreContractBasicInfo()
        {
            BEGDC.PreContract.BLL.IPreContractInfo iPreContractInfo = (BEGDC.PreContract.BLL.IPreContractInfo)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_PreContractInfo_Service];
            BEGDC.PreContract.DAL.PreContractInfo preContractInfo = iPreContractInfo.GetPreContractInfo(this.PreContractID);
            this.lblPreContractNo.Text = preContractInfo.PreContractNo;
            this.lblContractType.Text = preContractInfo.TypeID.DicItemValue;
            this.lblPreContractName.Text = preContractInfo.ContractName;
            this.lblProductName.Text = preContractInfo.ProductID.ProductName;
            this.lblCygnusAmount.Text = (preContractInfo.SalePrice / 1000000).ToString();
            this.lblOrigonAmout.Text = (preContractInfo.OrionPrice / 1000000).ToString();
            this.lblCygnusTransferredDate.Text = preContractInfo.PromisedDate.Value.ToShortDateString();
            this.lblOrigonTrabsferredDate.Text = preContractInfo.SignDue.Value.ToShortDateString();
            this.lblERPReceivedDate.Text = preContractInfo.ERPReceived.Value.ToShortDateString();
        }

        private void BindDataGrid()
        {
            BEGDC.PreContract.BLL.ISalesFeedback iSalesFeedback = (BEGDC.PreContract.BLL.ISalesFeedback)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_SalesFeedback_Service];

            DataTable dt = iSalesFeedback.GetFeedbackByPreContractID(this.PreContractID);
            this.gvFeedback.DataSource = dt.DefaultView;
            this.gvFeedback.DataBind();

            this.pgFeedback.Visible = true;
            this.pgFeedback.BindtoGridView(this.gvFeedback, dt.DefaultView);
            if (dt.Rows.Count == 0)
            {
                this.pgFeedback.Visible = false;
            }
        }

        protected void gvFeedback_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (!string.IsNullOrEmpty(e.CommandArgument.ToString()) && e.CommandName == "DeleteRow")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                GridViewRow row = this.gvFeedback.Rows[index];
                int feedbackID = Convert.ToInt32(this.gvFeedback.DataKeys[index].Value);

                BEGDC.PreContract.BLL.ISalesFeedback iSalesFeedback = (BEGDC.PreContract.BLL.ISalesFeedback)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_SalesFeedback_Service];
                iSalesFeedback.DeleteSalesFeedback(feedbackID);
                this.BindDataGrid();
            }
        }
    }
}